import time
from selenium.webdriver.common.by import By
from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://mail.163.com/')

# 添加登录需要的cookie
cookies = [
    {
        'name': 'NTES_SESS',
        'value': 'LlBvhphI_8jyfxqpqRvLV2LHsObYQiB53dijel6cf9cE2wCz2RvHFKNeLI8BPYDKSM8Tmo7x6LIgzmtyYsfxqtivmmh6QfE5J1_2adueD9X24WcQ6wXXqbq0kILJ0PeNUarNMpOx2gDYkcVWBy69DjNmtK2guyHJ7GEZhcgigbaB3WhlKeI0JpC8eNTElgzoHLRY.nht_mwVK'
    },
    {
        'name': 'Coremail',
        'value': 'b9a12f411aa37%zBifTLleXHNbUMlnmoeeAISBpOeYpBDZ%g4a83.mail.163.com'
    },
    {
        'name': 'MAIL_ENTRY_INFO',
        'value': '1|0|mail163|email163|183.94.151.6|63ec5a99fa60dfca07d7ad2c868a0361_v1|'
    },
    {
        'name': 'MAIL_ENTRY_CS',
        'value': '00ed98b3388989051e6c3075b7a267d2'
    },
    {
        'name': 'cm_last_info',
        'value': 'dT1pdDEzMjc3OTY4OTM1JTQwMTYzLmNvbSZkPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0R6QmlmVExsZVhITmJVTWxubW9lZUFJU0JwT2VZcEJEWiZzPXpCaWZUTGxlWEhOYlVNbG5tb2VlQUlTQnBPZVlwQkRaJmg9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20lMkZqczYlMkZtYWluLmpzcCUzRnNpZCUzRHpCaWZUTGxlWEhOYlVNbG5tb2VlQUlTQnBPZVlwQkRaJnc9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20mbD0tMSZ0PTcmYXM9dHJ1ZQ=='
    },

]
for cookiet1 in cookies:
    driver.add_cookie(cookiet1)
# 刷新浏览器
time.sleep(2)
driver.refresh()
# 定位写信按钮,点击跳转
time.sleep(2)
driver.find_element(By.ID, '_mail_component_149_149').click()
# 定位收件人输入框,输入收件人姓名
time.sleep(2)
driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('我的宝贝儿子孙宇桑')
# 定位时主题输入框,输入主题名
time.sleep(2)
theme = driver.find_element(By.XPATH, '//header/div[2]/div[1]/div/div/input')
theme.send_keys('我是孙宇爸爸')
# 定位内容输入框,输入内容)
time.sleep(2)
frame = driver.find_elements(By.TAG_NAME, 'iframe')[3]
driver.switch_to.frame(frame)
text = driver.find_element(By.CSS_SELECTOR,'body').send_keys('我是孙宇的爸爸')
# 退出frame
time.sleep(2)
driver.switch_to.default_content()
# 定位发送按钮,点击发送
time.sleep(2)
driver.find_element(By.XPATH, '//footer/div[1]/span[2]').click()
# 关闭浏览器
time.sleep(2)
driver.quit()
